package com.sofwin.controller;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.sofwin.pojo.Status;
import com.sofwin.service.StatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/status")
public class StatusController extends BaseController {
    @Autowired
    private StatusService statusService;

    @GetMapping("/getAllStatus")
    public TableDataInfo getAllStatus(@RequestParam(name = "pageCurrent",required = true,defaultValue = "1")Integer pageCurrent,
                                      @RequestParam(name = "pageSize",required = true,defaultValue = "5")Integer pageSize){
        Page<Status> page = statusService.page(new Page<Status>(pageCurrent, pageSize));
        return getDataTable(page.getRecords(),page.getTotal());
    }

    @PostMapping("/addOrUpdate")
    public AjaxResult addOrUpdateStatus(@RequestBody Status status) {
        LambdaQueryWrapper<Status> wrapper = new LambdaQueryWrapper<>();
        if (status.getSId() != null) {
            wrapper.eq(Status::getSId,status.getSId());
            Status one = statusService.getOne(wrapper);
            if (StringUtils.isNotEmpty(one.toString())) {
                statusService.updateById(status);
                return AjaxResult.success("修改成功");
            }
        }
        statusService.save(status);
        return AjaxResult.success("新增成功");
    }

    @DeleteMapping("delByIds")
    public AjaxResult delByIds(@RequestBody List<Integer> ids) {
        statusService.removeByIds(ids);
        return AjaxResult.success("删除成功");
    }
}
